import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/LoginView.vue"
import Home from "@/views/HomeView.vue"
import { Message } from 'element-ui'
Vue.use(VueRouter)
const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: "登录界面"
    }
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {
      title: "主界面"
    },
    children: [
      {
        path: "/home",
        redirect: "/home/welcome"
      },
      {
        path: "welcome",
        name: "welcome",
        meta: {
          title: "欢迎页面"
        },
        component: () => import("@/components/Welcome.vue")
      },
      {
        path: "businessList",
        name: "businessList",
        meta: {
          title: "商家列表"
        },
        component: () => import("@/components/BusinessList.vue")
      },
      {
        path: "businessUpdate",
        name: "businessUpdate",
        meta: {
          title: "商家修改"
        },
        component: () => import("@/components/BusinessUpdate.vue")
      },
      {
        path: "businessAdd",
        name: "businessAdd",
        meta: {
          title: "新增商家"
        },
        component: () => import("@/components/BusinessAdd.vue")
      },
      {
        path: "userList",
        name: "userList",
        meta: {
          title: "用户列表"
        },
        component: () => import("@/components/UserList.vue")
      },
      {
        path: "orderList",
        name: "orderList",
        meta: {
          title: "订单列表"
        },
        component: () => import("@/components/OrderList.vue")
      },
      {
        path: "diaryList",
        name: "diaryList",
        meta: {
          title: "日志列表"
        },
        component: () => import("@/components/DiaryList.vue")
      },
      {
        path: "diaryAdd",
        name: "diaryAdd",
        meta: {
          title: "新增日志"
        },
        component: () => import("@/components/DiaryAdd.vue")
      },
    ]
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.name == "login") {
    next()
  } else {
    let token = sessionStorage.getItem("token")
    if (token) {
      next()
    } else {
      Message.error("请先登录!")
      next("/login")
    }
  }
  document.title = to.meta.title
})

export default router
